package com.epmxx.controller;

import com.epmxx.common.Result;
import com.epmxx.domain.Hotel;
import com.epmxx.service.HotelService;
import com.epmxx.vo.HotelVO;
import com.epmxx.vo.RoomQuery;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 酒店控制类
 */
@RestController
@RequestMapping("hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @GetMapping("page.do")
    public Result page(RoomQuery query) {
        PageInfo<HotelVO> pageInfo = this.hotelService.queryPage(query);
        return new Result(pageInfo);
    }

    /**
     * 新增酒店信息
     *
     * @return
     */
    @PostMapping("add.do")
    public Result add(Hotel hotel) {
        this.hotelService.add(hotel);
        return new Result();
    }

    /**
     * 根据 ID 删除酒店信息
     *
     * @return
     */
    @PostMapping("delete.do")
    public Result add(Integer id) {
        this.hotelService.delete(id);
        return new Result();
    }

    /**
     * 修改酒店信息
     *
     * @return
     */
    @PostMapping("update.do")
    public Result update(Hotel hotel) {
        this.hotelService.update(hotel);
        return new Result();
    }

    /**
     * 批量删除酒店信息
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("deleteBatch.do")
    public Result deleteBatch(@RequestParam("id") List<Integer> id) {
        int count = this.hotelService.deleteHotelBatch(id);
        if (count >= 1) {
            return new Result(200, "批量删除成功");
        }
        return new Result(500, "批量删除失败");
    }
}
